package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/attributes")
@Api(tags = "05. 属性管理模块")
@Validated
@Slf4j
public class AttributeController {
    @Autowired
    private IAttributeService attributeService;

    // http://localhost:9999/attribute/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AttributeAddNewDTO attributeAddNewDTO) {
        log.debug("开始处理【添加属性】的请求,参数: {}", attributeAddNewDTO);
        attributeService.addNew(attributeAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9999/attributes/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除属性")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值!") Long id) {
        log.debug("开始处理【根据ID删除属性】的请求，参数：{}", id);
        attributeService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9999/attributes/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改属性")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfo(@PathVariable @Range(min = 1, message = "请提交有效的ID值!") Long id,
                                       @Valid AttributeUpdateDTO attributeUpdateDTO) {
        log.debug("开始处理【修改属性】的请求，ID：{}，新数据：{}", id, attributeUpdateDTO);
        attributeService.updateInfo(id, attributeUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9999/attributes/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询属性详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<AttributeStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID查询属性详情】的请求，参数：{}", id);
        AttributeStandardVO queryResult = attributeService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9999/attributes
    @GetMapping("")
    @ApiOperation("查询属性列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AttributeListItemVO>> list() {
        log.debug("开始处理【属性列表】的请求,无参数");
        List<AttributeListItemVO> list = attributeService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9999/attributes/list-by-templateId
    @GetMapping("/list-by-templateId")
    @ApiOperation("根据属性模板ID查询属性列表")
    @ApiOperationSupport(order = 430)
    public JsonResult<List<AttributeListItemVO>> list(Long templateId) {
        log.debug("开始处理【根据属性模板ID查询属性列表】的请求,参数: {}",templateId);
        List<AttributeListItemVO> list = attributeService.listByTemplateId(templateId);
        return JsonResult.ok(list);
    }

}
